package qsc.designpattern.proxy.JDKProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @auther QiuShangcheng
 * @create 2021/5/3
 */
public class MyJDKProxy {
    private UserService userService;

    public MyJDKProxy(UserService userService) {
        this.userService = userService;
    }

    public  UserService myJDKProxy(){
        UserService u = (UserService) Proxy.newProxyInstance(userService.getClass().getClassLoader(), userService.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println("准备要执行saveUser（）了。");
                        Object obj = method.invoke((userService));
                        System.out.println("执行完毕saveUser（）了。");
                        return obj;

                    }
                });
        return u;
    }
}
